﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class CX_ZHJGControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridList1;
        DropDownList dll区属;
        TextBox 立项号,项目名称;
        Button CX ;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CX = (Button)FindControl("CX");
            dll区属 = (DropDownList)FindControl("dll区属");
            立项号 = (TextBox)FindControl("立项号");
            项目名称 = (TextBox)FindControl("项目名称");
            if (!base.Page.IsPostBack)
            {
                BindDll();
                BindData();
            }
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            CX.Click += new EventHandler(CX_Click);
        }

        protected void CX_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void BindDll()
        {
            dll区属.Items.Add(new ListItem("全部", ""));
            string sql = "Select keyname From zs_params Where Name ='区属'";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                string strQS = dr["keyname"].ToString();
                dll区属.Items.Add(new ListItem(strQS, strQS));
            }
        }
        protected void BindData()
        {
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            string strwhere = string.Empty;
            if (!string.IsNullOrEmpty(dll区属.SelectedValue) )
            {
                strwhere += " and 区属 = '" + dll区属.SelectedValue + "'";
            }
            if (!string.IsNullOrEmpty(立项号.Text.Trim()))
            {
                strwhere += " and 立项号 like '%" + 立项号.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(项目名称.Text.Trim()))
            {
                strwhere += " and 项目名称 like '%" + 项目名称.Text.Trim() + "%'";
            }
            if (HttpContext.Current.Request["PROID"] != null)
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["PROID"].ToString()))
                {
                    strwhere += " and ID = '" + HttpContext.Current.Request["PROID"].ToString() + "'";
                }
            }
            string sqlrole = "Select rolename From zs_role  Where Id In (Select roleid  From zs_user_in_role Where userid = '" + userid + "') and rolename like '%征收部门%'";
            DataTable dtrole = _EluZSDataProvider.GetTable(sqlrole);
            string role = string.Empty;
            if (dtrole.Rows.Count > 0)
            {
                role = dtrole.Rows[0]["rolename"].ToString();
                string rolestr = role.Substring(0, 2);
                strwhere += " and t.区属 like '%" + rolestr + "%'";
            }
            //string sql = "select t.id,t.项目名称,t.立项号 from Zs_Project t Where t.id In (Select pid From zs_prono Where 审核状态 = 1)";
            string sql = "select t.id,t.项目名称,t.立项号 from Zs_Project t Where 1=1 ";
            sql += strwhere;
            sql += " order by addtime desc ";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.DataBind();
        }
        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string strId = rowview["ID"].ToString();
                Literal 评估机构选定 = (Literal)e.Row.FindControl("评估机构选定");
                Literal 已评估户数 = (Literal)e.Row.FindControl("已评估户数");
                
                Literal 总权证户数 = (Literal)e.Row.FindControl("总权证户数");
                Literal 已签协议数 = (Literal)e.Row.FindControl("已签协议数");
                Literal 已签占总户数百分比 = (Literal)e.Row.FindControl("已签占总户数百分比");
                Literal 征收决定复议 = (Literal)e.Row.FindControl("征收决定复议");
                Literal 征收决定诉讼 = (Literal)e.Row.FindControl("征收决定诉讼");

                Literal 补偿决定 = (Literal)e.Row.FindControl("补偿决定");
                Literal 补偿决定复议 = (Literal)e.Row.FindControl("补偿决定复议");
                Literal 补偿决定诉讼 = (Literal)e.Row.FindControl("补偿决定诉讼");

                Literal 司法申请 = (Literal)e.Row.FindControl("司法申请");
                Literal 司法强制 = (Literal)e.Row.FindControl("司法强制");

                评估机构选定.Text = _EluZSDataProvider.GetString(@"Select Case When (Select Count(*) From zs_prono Where pid='" + strId + "' And 征收评估机构 Is Not Null)>0 Then '√' Else '  ' End  From dual ");
                //已评估户数.Text = _EluZSDataProvider.GetString("Select count(Distinct zspid) From zs_zzbcjfenhu Where pid In (Select Id From zs_prono Where pid='" + strId + "')");
                已评估户数.Text = _EluZSDataProvider.GetString("Select Count(Distinct Id) From zs_zzbcjfenhu Where pid In (Select Id From zs_prono Where pid='" + strId + "') And zspid In (Select Id From zs_zsperson Where pid='" + strId + "')");
                int intSum1 = Convert.ToInt32(_EluZSDataProvider.GetString("Select Count(Id) From zs_zsperson Where pid ='" + strId + "'"));
                //int intSum2 = Convert.ToInt32(_EluZSDataProvider.GetString("Select count(*) From zs_zzbcjfenhu Where pid In (Select Id From zs_prono Where pid='" + strId + "')"));
                //int newSum = 0;
                //if (intSum1 > intSum2) { newSum = intSum1; } else if (intSum1 < intSum2) { newSum = intSum2; } else { newSum = intSum1; }
                总权证户数.Text = intSum1.ToString();
                int yqxys = Convert.ToInt32(_EluZSDataProvider.GetString("select ((Select Count(*) From Zs_Hbhxy Where Pnid In (Select Id From Zs_Prono Where Pid = '" + strId + "')) +(Select Count(*) From Zs_Cqzhxy Where Fid In (Select Id From Zs_Proloupan Where Zid In (Select Id From Zs_Prozhuang Where Pnid = '" + strId + "'))))  from dual"));
                //已签协议数.Text = yqxys.ToString();
                double hushu1 = 0;
                //已签协议人员总数
                int yqxyrys = Convert.ToInt32(_EluZSDataProvider.GetString(@"Select Count(Id) From zs_zsperson Where Id In (
  Select personid From zs_hbhxy Where pnid In (
  Select Id From zs_prono Where pid='" + strId + @"')
Union All   
  Select zspid From zs_proloupan Where zid In 
  (Select Id From zs_prozhuang Where pnid='" + strId + @"')
  And Id In (Select fid From zs_cqzhxy ))
    And pid= '" + strId + @"'"));
                已签协议数.Text = yqxyrys.ToString();
                if (!string.IsNullOrEmpty(yqxyrys.ToString())) { hushu1 = Convert.ToDouble(yqxyrys); }
                double hushu2 = 1;
                if (!string.IsNullOrEmpty(intSum1.ToString())) { hushu2 = Convert.ToDouble(intSum1); }
                if (hushu2 == 0) { 已签占总户数百分比.Text = "%"; }
                else
                {
                    double hushubaifenbi = hushu1 / hushu2;
                    已签占总户数百分比.Text = Math.Round(hushubaifenbi,4) * 100 + "%";
                }


                征收决定复议.Text = _EluZSDataProvider.GetString("Select Case When (Select Count(*) From ZS_XZFY Where pnid In (Select Id From zs_prono Where pid='" + strId + "'))>0 Then '√' Else '' End  From dual ");
                征收决定诉讼.Text = _EluZSDataProvider.GetString(@"Select Case When 在审状态='0' Then '一审' 
When 在审状态='1' Then '二审'
When 在审状态='2' Then '再审' Else '' End   
From ZS_XZSS Where pnid In (Select Id From zs_prono 
Where pid='" + strId + "')  Order By to_number(nvl(在审状态,0)) Desc ");

                补偿决定.Text = _EluZSDataProvider.GetString(@"Select Count(IID) From st_instance Where wid In (
Select wid From st_workflow Where wname ='房屋征收补偿决定')
And iid In (Select iid From xt_instance_ext Where 关联ID In (
Select Id From zs_zzbcjfenhu Where PID In (
Select Id From zs_prono Where pid='" + strId + "')))");
                补偿决定复议.Text = _EluZSDataProvider.GetString(@"Select count(Distinct Id) From zs_BCJDXZFY Where caseid In (
Select caseid From zs_case_register Where iid In (
Select IID From st_instance Where wid In (
Select wid From st_workflow Where wname ='房屋征收补偿决定')
And iid In (Select iid From xt_instance_ext Where 关联ID In (
Select Id From zs_zzbcjfenhu Where PID In (
Select Id From zs_prono Where pid='" + strId + "')))))");
                补偿决定诉讼.Text = _EluZSDataProvider.GetString(@"Select count(Distinct Id) From ZS_ONEJUSTICE Where caseid In (
Select caseid From zs_case_register Where iid In (
Select IID From st_instance Where wid In (
Select wid From st_workflow Where wname ='房屋征收补偿决定')
And iid In (Select iid From xt_instance_ext Where 关联ID In (
Select Id From zs_zzbcjfenhu Where PID In (
Select Id From zs_prono Where pid='" + strId + "')))))");
                司法申请.Text = _EluZSDataProvider.GetString(@"Select count(Distinct Id) From ZS_SFSQXX Where caseid In (
Select caseid From zs_case_register Where iid In (
Select IID From st_instance Where wid In (
Select wid From st_workflow Where wname ='房屋征收补偿决定')
And iid In (Select iid From xt_instance_ext Where 关联ID In (
Select Id From zs_zzbcjfenhu Where PID In (
Select Id From zs_prono Where pid='" + strId + "')))))");
                司法强制.Text = _EluZSDataProvider.GetString(@"Select count(Distinct Id) From ZS_QZZXXX Where caseid In (
Select caseid From zs_case_register Where iid In (
Select IID From st_instance Where wid In (
Select wid From st_workflow Where wname ='房屋征收补偿决定')
And iid In (Select iid From xt_instance_ext Where 关联ID In (
Select Id From zs_zzbcjfenhu Where PID In (
Select Id From zs_prono Where pid='" + strId + "')))))");
            }
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
